home *** CD-ROM | disk | FTP | other *** search
/ Isometric Game Programming with DirectX 7.0 / Isometric Game Programming.iso / source / chapter24 / isohex24_2 / isohex24_2.cpp < prev    next >
C/C++ Source or Header  |  2000-12-28  |  10KB  |  375 lines

  1. /*****************************************************************************
  2. IsoHex24_2.cpp
  3. Ernest S. Pazera
  4. 16NOV2000
  5. Start a WIN32 Application Workspace, add in this file
  6. Needs ddraw.lib, d3d8.lib and dxguid.lib
  7. Needs GDICanvas.h/cpp
  8. Needs DDFuncs.h/cpp
  9. *****************************************************************************/
  10.  
  11. //////////////////////////////////////////////////////////////////////////////
  12. //INCLUDES
  13. //////////////////////////////////////////////////////////////////////////////
  14. #define WIN32_LEAN_AND_MEAN  
  15.  
  16. #include <windows.h>   
  17. #include <math.h>//sin and cos
  18. #include "GDICanvas.h"
  19. #include "ddraw.h"
  20. #include "DDFuncs.h"
  21. #include "d3d.h"
  22.  
  23. //////////////////////////////////////////////////////////////////////////////
  24. //DEFINES
  25. //////////////////////////////////////////////////////////////////////////////
  26. //name for our window class
  27. #define WINDOWCLASS "ISOHEX24"
  28. //title of the application
  29. #define WINDOWTITLE "IsoHex 24-2"
  30.  
  31. //screen attributes
  32. const DWORD SCREENWIDTH=640;
  33. const DWORD SCREENHEIGHT=480;
  34. const DWORD SCREENBPP=16;
  35.  
  36. //pi
  37. const double PI=3.14159;
  38.  
  39. //////////////////////////////////////////////////////////////////////////////
  40. //PROTOTYPES
  41. //////////////////////////////////////////////////////////////////////////////
  42. bool Prog_Init();//game data initalizer
  43. void Prog_Loop();//main game loop
  44. void Prog_Done();//game clean up
  45.  
  46. //////////////////////////////////////////////////////////////////////////////
  47. //GLOBALS
  48. //////////////////////////////////////////////////////////////////////////////
  49. HINSTANCE hInstMain=NULL;//main application handle
  50. HWND hWndMain=NULL;//handle to our main window
  51.  
  52. //IDirectDraw7 Pointer
  53. LPDIRECTDRAW7 lpdd=NULL;
  54.  
  55. //surfaces
  56. LPDIRECTDRAWSURFACE7 lpddsPrime=NULL;
  57. LPDIRECTDRAWSURFACE7 lpddsBack=NULL;
  58. LPDIRECTDRAWSURFACE7 lpddsTex=NULL;//texture surface
  59.  
  60. //IDirect3D7
  61. LPDIRECT3D7 lpd3d=NULL;
  62.  
  63. //IDirect3DDevice
  64. LPDIRECT3DDEVICE7 lpd3ddev=NULL;
  65.  
  66. //vertices
  67. D3DTLVERTEX vert[4];//three vertices
  68. //angle, used for vertex calculations
  69. double angle=0.0;
  70.  
  71. //////////////////////////////////////////////////////////////////////////////
  72. //WINDOWPROC
  73. //////////////////////////////////////////////////////////////////////////////
  74. LRESULT CALLBACK TheWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
  75. {
  76.     //which message did we get?
  77.     switch(uMsg)
  78.     {
  79.     case WM_KEYDOWN:
  80.         {
  81.             //check for escape key
  82.             if(wParam==VK_ESCAPE)
  83.             {
  84.                 DestroyWindow(hWndMain);
  85.             }
  86.  
  87.             return(0);//handled message
  88.         }break;
  89.     case WM_DESTROY://the window is being destroyed
  90.         {
  91.  
  92.             //tell the application we are quitting
  93.             PostQuitMessage(0);
  94.  
  95.             //handled message, so return 0
  96.             return(0);
  97.  
  98.         }break;
  99.     case WM_PAINT://the window needs repainting
  100.         {
  101.             //a variable needed for painting information
  102.             PAINTSTRUCT ps;
  103.             
  104.             //start painting
  105.             HDC hdc=BeginPaint(hwnd,&ps);
  106.  
  107.             /////////////////////////////
  108.             //painting code would go here
  109.             /////////////////////////////
  110.  
  111.             //end painting
  112.             EndPaint(hwnd,&ps);
  113.                         
  114.             //handled message, so return 0
  115.             return(0);
  116.         }break;
  117.     }
  118.  
  119.     //pass along any other message to default message handler
  120.     return(DefWindowProc(hwnd,uMsg,wParam,lParam));
  121. }
  122.  
  123.  
  124. //////////////////////////////////////////////////////////////////////////////
  125. //WINMAIN
  126. //////////////////////////////////////////////////////////////////////////////
  127. int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
  128. {
  129.     //assign instance to global variable
  130.     hInstMain=hInstance;
  131.  
  132.     //create window class
  133.     WNDCLASSEX wcx;
  134.  
  135.     //set the size of the structure
  136.     wcx.cbSize=sizeof(WNDCLASSEX);
  137.  
  138.     //class style
  139.     wcx.style=CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  140.  
  141.     //window procedure
  142.     wcx.lpfnWndProc=TheWindowProc;
  143.  
  144.     //class extra
  145.     wcx.cbClsExtra=0;
  146.  
  147.     //window extra
  148.     wcx.cbWndExtra=0;
  149.  
  150.     //application handle
  151.     wcx.hInstance=hInstMain;
  152.  
  153.     //icon
  154.     wcx.hIcon=LoadIcon(NULL,IDI_APPLICATION);
  155.  
  156.     //cursor
  157.     wcx.hCursor=LoadCursor(NULL,IDC_ARROW);
  158.  
  159.     //background color
  160.     wcx.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
  161.  
  162.     //menu
  163.     wcx.lpszMenuName=NULL;
  164.  
  165.     //class name
  166.     wcx.lpszClassName=WINDOWCLASS;
  167.  
  168.     //small icon
  169.     wcx.hIconSm=NULL;
  170.  
  171.     //register the window class, return 0 if not successful
  172.     if(!RegisterClassEx(&wcx)) return(0);
  173.  
  174.     //create main window
  175.     hWndMain=CreateWindowEx(0,WINDOWCLASS,WINDOWTITLE, WS_POPUP | WS_VISIBLE,0,0,320,240,NULL,NULL,hInstMain,NULL);
  176.  
  177.     //error check
  178.     if(!hWndMain) return(0);
  179.  
  180.     //if program initialization failed, then return with 0
  181.     if(!Prog_Init()) return(0);
  182.  
  183.     //message structure
  184.     MSG msg;
  185.  
  186.     //message pump
  187.     for(;;)    
  188.     {
  189.         //look for a message
  190.         if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  191.         {
  192.             //there is a message
  193.  
  194.             //check that we arent quitting
  195.             if(msg.message==WM_QUIT) break;
  196.             
  197.             //translate message
  198.             TranslateMessage(&msg);
  199.  
  200.             //dispatch message
  201.             DispatchMessage(&msg);
  202.         }
  203.  
  204.         //run main game loop
  205.         Prog_Loop();
  206.     }
  207.     
  208.     //clean up program data
  209.     Prog_Done();
  210.  
  211.     //return the wparam from the WM_QUIT message
  212.     return(msg.wParam);
  213. }
  214.  
  215. //////////////////////////////////////////////////////////////////////////////
  216. //INITIALIZATION
  217. //////////////////////////////////////////////////////////////////////////////
  218. bool Prog_Init()
  219. {
  220.     lpdd=LPDD_Create(hWndMain,DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_ALLOWREBOOT);
  221.  
  222.     //set the display mode
  223.     lpdd->SetDisplayMode(SCREENWIDTH,SCREENHEIGHT,SCREENBPP,0,0);
  224.  
  225.     //create primary surface
  226.     DDSURFACEDESC2 ddsd;
  227.     memset(&ddsd,0,sizeof(DDSURFACEDESC2));
  228.     ddsd.dwSize=sizeof(DDSURFACEDESC2);
  229.     ddsd.dwFlags=DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
  230.     ddsd.dwBackBufferCount=1;
  231.     ddsd.ddsCaps.dwCaps=DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX | DDSCAPS_3DDEVICE;
  232.     lpdd->CreateSurface(&ddsd,&lpddsPrime,NULL);
  233.  
  234.     //create back buffer
  235.     DDSCAPS2 ddscaps;
  236.     memset(&ddscaps,0,sizeof(DDSCAPS2));
  237.     ddscaps.dwCaps=DDSCAPS_BACKBUFFER | DDSCAPS_3DDEVICE;
  238.     lpddsPrime->GetAttachedSurface(&ddscaps,&lpddsBack);
  239.  
  240.     //create the texture surface
  241.     memset(&ddsd,0,sizeof(DDSURFACEDESC2));
  242.     ddsd.dwSize=sizeof(DDSURFACEDESC2);
  243.     ddsd.dwFlags=DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
  244.     ddsd.dwWidth=64;
  245.     ddsd.dwHeight=64;
  246.     ddsd.ddsCaps.dwCaps=DDSCAPS_TEXTURE;
  247.     lpdd->CreateSurface(&ddsd,&lpddsTex,NULL);
  248.     //used ddfuncs to load a bitmap onto the texture
  249.     LPDDS_ReloadFromFile(lpddsTex,"texture.bmp");
  250.  
  251.     //get the idirect3d pointer
  252.     lpdd->QueryInterface(IID_IDirect3D7,(void**)&lpd3d);//ICKY COM STUFF!
  253.  
  254.     //create the idirect3ddevice(hack method)
  255.     if(FAILED(lpd3d->CreateDevice(IID_IDirect3DTnLHalDevice,lpddsBack,&lpd3ddev)))//try tnl
  256.         if(FAILED(lpd3d->CreateDevice(IID_IDirect3DHALDevice,lpddsBack,&lpd3ddev)))//no tnl, try hal
  257.             if(FAILED(lpd3d->CreateDevice(IID_IDirect3DMMXDevice,lpddsBack,&lpd3ddev)))//no hal, try mmp
  258.                 if(FAILED(lpd3d->CreateDevice(IID_IDirect3DRGBDevice,lpddsBack,&lpd3ddev)))//no mmx, resort to rgb
  259.                     return(false);//problem, return false
  260.  
  261.  
  262.     //set up viewport
  263.     D3DVIEWPORT7 vp;
  264.     vp.dwX=0;
  265.     vp.dwY=0;
  266.     vp.dwWidth=SCREENWIDTH;
  267.     vp.dwHeight=SCREENHEIGHT;
  268.     vp.dvMinZ=0.0;
  269.     vp.dvMaxZ=1.0;
  270.  
  271.     //set viewport for device
  272.     lpd3ddev->SetViewport(&vp);
  273.  
  274.     //initialize the vertices(partially, anyway)
  275.     vert[0].color=D3DRGB(0.25,0.25,0.25);//set the color for this vertex
  276.     vert[0].specular=0;//zero for specular
  277.     vert[0].rhw=1.0;//rhw is 1.0
  278.     vert[0].tu=0.0;//0.0 for both texture coordinates
  279.     vert[0].tv=0.0;
  280.     vert[0].sz=0.5;//static z value
  281.  
  282.     vert[1].color=D3DRGB(0.5,0.5,0.5);//set the color for this vertex
  283.     vert[1].specular=0;//zero for specular
  284.     vert[1].rhw=1.0;//rhw is 1.0
  285.     vert[1].tu=1.0;//0.0 for both texture coordinates
  286.     vert[1].tv=0.0;
  287.     vert[1].sz=0.5;//static z value
  288.  
  289.     vert[2].color=D3DRGB(0.5,0.5,0.5);//set the color for this vertex
  290.     vert[2].specular=0;//zero for specular
  291.     vert[2].rhw=1.0;//rhw is 1.0
  292.     vert[2].tu=0.0;//0.0 for both texture coordinates
  293.     vert[2].tv=1.0;
  294.     vert[2].sz=0.5;//static z value
  295.  
  296.     vert[3].color=D3DRGB(1.0,1.0,1.0);//set the color for this vertex
  297.     vert[3].specular=0;//zero for specular
  298.     vert[3].rhw=1.0;//rhw is 1.0
  299.     vert[3].tu=1.0;//0.0 for both texture coordinates
  300.     vert[3].tv=1.0;
  301.     vert[3].sz=0.5;//static z value
  302.  
  303.     //set the texture
  304.     lpd3ddev->SetTexture(0,lpddsTex);
  305.  
  306.     return(true);//return success
  307. }
  308.  
  309. //////////////////////////////////////////////////////////////////////////////
  310. //CLEANUP
  311. //////////////////////////////////////////////////////////////////////////////
  312. void Prog_Done()
  313. {    
  314.     //release IDirect3DDevice
  315.     if(lpd3ddev)
  316.     {
  317.         lpd3ddev->Release();
  318.         lpd3ddev=NULL;
  319.     }
  320.  
  321.     //release IDirect3D 
  322.     if(lpd3d)
  323.     {
  324.         lpd3d->Release();
  325.         lpd3d=NULL;
  326.     }
  327.     
  328.     //get rid of the texture.
  329.     LPDDS_Release(&lpddsTex);
  330.  
  331.     //clean up primary surface(this will clean up the back buffer, also)
  332.     LPDDS_Release(&lpddsPrime);
  333.  
  334.     //clean up the dd pointer
  335.     LPDD_Release(&lpdd);
  336. }
  337.  
  338. //////////////////////////////////////////////////////////////////////////////
  339. //MAIN GAME LOOP
  340. //////////////////////////////////////////////////////////////////////////////
  341. void Prog_Loop()
  342. {
  343.     //set up the vertex positions
  344.     vert[0].sx=cos(angle)*240.0+320.0;
  345.     vert[0].sy=sin(angle)*240.0+240.0;
  346.  
  347.     vert[1].sx=cos(angle+PI/2)*240.0+320.0;
  348.     vert[1].sy=sin(angle+PI/2)*240.0+240.0;
  349.  
  350.     vert[2].sx=cos(angle-PI/2)*240.0+320.0;
  351.     vert[2].sy=sin(angle-PI/2)*240.0+240.0;
  352.  
  353.     vert[3].sx=cos(angle-PI)*240.0+320.0;
  354.     vert[3].sy=sin(angle-PI)*240.0+240.0;
  355.  
  356.     //add to the angle for next time
  357.     angle+=PI/180;
  358.  
  359.     //clear the viewport to black
  360.     lpd3ddev->Clear(0,NULL,D3DCLEAR_TARGET,0,0,0);
  361.  
  362.     //start the scene
  363.     lpd3ddev->BeginScene();
  364.  
  365.     //draw the triangle
  366.     lpd3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP,D3DFVF_TLVERTEX,vert,4,0);
  367.  
  368.     //end the scene
  369.     lpd3ddev->EndScene();
  370.  
  371.     //flip 
  372.     lpddsPrime->Flip(NULL,DDFLIP_WAIT);
  373. }
  374.  
  375.